/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: linux_atomic.c
 *
 * Purpose: virtual os layer atomic operation interface linux implement 
 *
 * Developer:
 *   wen.gu , 2015-06-09
 *
 * TODO: 
 *
 ***************************************************************************/
//inner header file
#include "sys_config.h"

#if defined(G_OS_LINUX)
//API header file
#include "vos_atomic.h"
#include "vos_mutex.h"

/***************************************************************************
*
* windows atomic operation API define
*
***************************************************************************/
#if defined(__GNUC__)
#include <pthread.h>

GS32 vosAtomicExchangeAdd(GS32 volatile *orgValue, GS32 addValue)
{
    return __sync_fetch_and_add(orgValue, addValue);
}

GS32 vosAtomicIncrement(GS32 volatile *orgValue)
{
    return __sync_fetch_and_add(orgValue, 1);
}

GS32 vosAtomicDecrement(GS32 volatile *orgValue)
{
    return __sync_fetch_and_sub(orgValue, 1);
}

GS32 vosAtomicOr(GS32 volatile *orgValue, GS32 value)
{
    return __sync_fetch_and_or(orgValue, value);
}

GS32 vosAtomicAnd(GS32 volatile *orgValue, GS32 value)
{
    return __sync_fetch_and_and(orgValue, value);
}

GS32 vosAtomicCmpExchange(GS32 volatile *orgValue, GS32 old_value, GS32 new_value)
{
#ifdef __ARMCC_VERSION
        // armcc will throw an error if ptr is not an integer type
        volatile uintptr_t *tmp = (volatile uintptr_t*)orgValue;
        return (void*)__sync_bool_compare_and_swap(tmp, old_value, new_value);
#else
        return __sync_bool_compare_and_swap(orgValue, old_value, new_value);
#endif

}

//////////////////////////////////////////////////////////////////////////
#if defined(__arm__)
#define USE_FAKE_ATOMIC_OPT_64
#endif

#if defined(USE_FAKE_ATOMIC_OPT_64)
pthread_mutex_t g_atomic_mutex_lock = PTHREAD_MUTEX_INITIALIZER;

#define VOS_ATOMIC_MUTEX           g_atomic_mutex_lock
#define vosGlobalMutexLock(lock)   pthread_mutex_lock(&(lock))
#define vosGlobalMutexUnlock(lock) pthread_mutex_unlock(&(lock))
#endif

GS64 vosAtomicExchangeAdd64(GS64 volatile *orgValue, GS64 addValue)
{
#if defined(USE_FAKE_ATOMIC_OPT_64)
//todo fixme, use really atomic operation interface
    GS64 oldValue;
    vosGlobalMutexLock(VOS_ATOMIC_MUTEX);
    oldValue = *orgValue;
    *orgValue += addValue;
    vosGlobalMutexUnlock(VOS_ATOMIC_MUTEX);

    return oldValue;
#else
    return __sync_fetch_and_add(orgValue, addValue);
#endif

}

GS64 vosAtomicIncrement64(GS64 volatile *orgValue)
{
#if defined(USE_FAKE_ATOMIC_OPT_64)
    return vosAtomicExchangeAdd64(orgValue, 1);
#else
    return __sync_fetch_and_add(orgValue, 1);
#endif
}

GS64 vosAtomicDecrement64(GS64 volatile *orgValue)
{
#if defined(USE_FAKE_ATOMIC_OPT_64)
    return vosAtomicExchangeAdd64(orgValue, -1);
#else 
    return __sync_fetch_and_add(orgValue, -1);
#endif
}
#else
#error "process other case(not gcc)"
#endif
#endif //end of defined(G_OS_LINUX)
